<?php

use GatewayWorker\Lib\Gateway;

require_once __DIR__.'./../logic/MessageCode.php';
require_once __DIR__.'./../logic/MessageCode.php';

class RoomDetail
{

    public $roomId;//房间id
    public $playerId;//玩家id
    public $roomStatus; //1游戏中,0空闲
    public $playerList = [];//当前玩家列表
    public $playerVote = [];//玩家投票的二位数组,key为玩家ID ,投票状态 0 未投票[1000=>0] 1同意[1001=>1] 2拒绝[1002=>2]
    public $playerReady = [];//玩家准备，key为玩家ID ,准备状态  1准备[1001=>1] 2未准备[1002=>2]
    public $roomCard = [];//房间当前手牌
    public $roomUsedCard;//已经用过的手牌


    public function __construct($roomId)
    {
        $this->roomId = $roomId;
        $this->roomStatus = 0;

    }

    /**
     * 获取状态
     */
    public function getRoomState(){
        $stateMessage=[
            'roomID'=>$this->roomId,
            'roomStatus'=>$this->roomStatus,
            'playerList'=>$this->playerList,
            'playerNumber'=>count($this->playerList),
        ];
        return $stateMessage;
    }

    /**
     * 检测是否已经加入过
     */
    public function checkPlayerIsJoinRoom($player_id){
        if(!empty($this->playerList[$player_id])){
            return true;
        }
        return false;
    }

    /**
     * chenming 2018-07-13
     * 玩家加入房间
     * @return string
     * @throws Exception
     */
    public function playerJoin($client_id,$playerId, $playerDetail)
    {
        if ($this->roomStatus == 1) {
            $message=ProtocolApi::returnMessage(ProtocolApi::$STATUS_ERROR,'房间游戏已开始');
            Gateway::sendToClient($client_id, $message);
            return true;
        }
        if (count($this->playerList) == 3) {
            $message=ProtocolApi::returnMessage(ProtocolApi::$STATUS_ERROR,'房间人数已满');
            Gateway::sendToClient($client_id, $message);
            return true;
        }
        if (!empty($this->playerList[$playerId])) {
            echo 'playerJoin';
            $message=ProtocolApi::returnMessage(ProtocolApi::$STATUS_ERROR,'您已在房间中');
            Gateway::sendToClient($client_id, $message);
            return true;
        }

        //更新状态
        $this->playerList[$playerId] = $playerDetail;
        $this->playerReady[$playerId] = [$playerId => 2];
        SessionMessage::updateClientidToSession($client_id,'room_id',$this->roomId);
        UserDb::updateUserTRoomId($playerId,$this->roomId);
        Gateway::joinGroup($client_id, $this->roomId);
        echo "加组成功 $client_id 加入 $this->roomId \r\n";
        $message=ProtocolApi::returnMessage(ProtocolApi::$DDZ_JOIN_ROOM,['RoomID'=>$this->roomId]);
        Gateway::sendToClient($client_id, $message);
        $message=ProtocolApi::returnMessage(ProtocolApi::$DDZ_GET_ROOM_DETAIL,$message);
        Gateway::sendToClient($client_id, $message);

        //群发通知
        $message=$this->getRoomState();
        $message=ProtocolApi::returnMessage(ProtocolApi::$DDZ_GET_ROOM_DETAIL,$message);
        Gateway::sendToGroup($this->roomId,$message);
        echo "消息群发成功 $this->roomId \r\n";
        return true;
    }

    /**
     * @param $playerId
     * @return int
     */
    public function playerOut($playerId)
    {

        unset($this->playerList[$playerId]);
        $this->roomStatus = 0;
        return MessageCode::$STATUS_OUT_SUCCESS;
    }

    /**
     * @param $playerId
     * @param array $readyDetail
     * @return int
     */
    public function playerReady($playerId, $readyDetail = [1002 => 2])
    {
        $this->playerReady[$playerId] = $readyDetail;
        $status = array_column($this->playerReady, '1001');
        if (count($this->playerList) == 3) {
            if (count($status) == 3) {
                $this->roomStatus = 1;
                return MessageCode::$STATUS_GAME_START;
            } else {
                return MessageCode::$STATUS_VOTE_FAIL;
            }
        } else {
            return MessageCode::$STATUS_NO_FULL;
        }


    }

    /**
     * chenming 2018-07-13
     * 房间玩家列表
     * @param $roomId
     * @return string
     */
    public function playerList()
    {
        return $this->playerList;

    }

    /**
     * chenming 2018-07-13
     * 房间发起投票
     */
    public function playerVote($playerId, $voteDetail = [1000 => 0])
    {
        //判断是否投过票
        if (isset($this->playerVote[$playerId])) {
            return MessageCode::$STATUS_VOTE;
        }
        $this->playerVote[$playerId] = $voteDetail;
        $status = array_column($this->playerVote, '1001');
        if (count($status) >= 2) {
            //投票通过，游戏解散
            $this->roomStatus = 0;
            return MessageCode::$STATUS_VOTE_SUCCESS;

        } else {
            return MessageCode::$STATUS_VOTE_FAIL;
        }
    }

    /**
     * chenming 2018-07-13
     * 房间玩家当前手牌
     * @param $playerId
     * @param $roomId
     * @return string
     */
    public function CurrentCard($playerId, $cardDetail)
    {
        $this->roomCard[$playerId] = $cardDetail;
        return $this->roomCard;
    }

    /**
     * chenming 2018-07-13
     * 房间已经用过的手牌
     * @param $playerId
     * @param $roomId
     * @return string
     */
    public function usedCard($cardDetail)
    {
        return $this->roomUsedCard = $cardDetail;
    }
}